######### 调用示例 ###################
# sh run-hzero-k8s.sh hzero-register
# sh run-hzero-k8s.sh hzero-register/
#
# K8S自动部署HZERO微服务
######################################

if [ -z "$1" ]; then
  echo "请输入HZERO服务名称："
  echo "example: sh run-hzero-k8s.sh hzero-register"
  exit 1
fi
SERVICE_NAME=${1,,} #参数转换成小写字符
SERVICE_NAME=${SERVICE_NAME%"/"} #删除末尾的"/"符

sudo yum install -y unzip
if [ ! -f k8s-deploy.zip ];then
  echo ">>> 下载 k8s-deploy.zip 文件"
  wget https://file.open.hand-china.com/hsop-hsop/hsop/video/3/da46cc67e64b4ef8b634cfa0ebc9f591/k8s-deploy.zip
fi
if [ ! -d k8s-deploy ];then
  echo ">>> 解压 k8s-deploy.zip 文件"
  unzip k8s-deploy.zip
fi

echo ">>> 创建hzero服务的存储命名空间：hzero"
kubectl create ns hzero

echo ">>> 开始部署服务：$SERVICE_NAME"
cd k8s-deploy/micro
echo ">>> 编辑部署配置文件：$SERVICE_NAME.yaml" && sleep 3
vim $SERVICE_NAME.yaml
echo ">>> 开始部署 $SERVICE_NAME 服务..." && sleep 3
kubectl apply -f $SERVICE_NAME.yaml -n hzero
sleep 5

echo ">>> 查看Pod情况：kubectl get po -n hzero"
echo ">>> 需等待Pod状态变为：Running，则说明部署及启动成功！"
kubectl get po -n hzero

if [ $SERVICE_NAME == "hzero-register" ] || [ $SERVICE_NAME == "hzero-gateway" ] || [ $SERVICE_NAME == "hzero-front" ]; then
  echo ">>> 查看service网络情况：kubectl get service -n hzero"
  kubectl get service -n hzero
fi
